<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends CommonController {
    public function index()
    {
        $phone = trim(I('post.phone'));
        if (!empty($phone)) {
            $preg_js = "/^1[3458][0-9]{9}$/";
            $pass_sj = preg_match($preg_js, $phone);
            if ($pass_sj) {
                //为手机
                $user = M('gy_user')->where(['phone'=>$phone])->select();
            }else {
                $user = M('gy_user')->where(['name'=>$phone])->select();
            }
            //数量
            $count = M('gy_user')->where(['phone'=>$phone])->count();

    	}else {

            //分页显示
            $gy_user = M('gy_user'); // 实例化User对象
            $count      = $gy_user->count();// 查询满足要求的总记录数
            $Page       = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $show       = $Page->show();// 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $user = $gy_user->order('time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
            $this->assign('page',$show);// 赋值分页输出
		}

        $this->assign('count',$count);
		$this->assign('user',$user);
        $this->display();
    }

    /**
     * 导出 user
     */
    public function gy_user_export(){
        $nm_list = M('gy_user')->select();
        $subject="Excel导出-用户表";
        $title=array("id","姓名","手机","密码","头像","捐赠时间");
        exportExcel($nm_list,$title,$subject); 
    }

    //编辑
    public function edit()
    {   
    	if (IS_POST) {
    		$id = I('post.id');
    		$password = md5(I('post.password'));

    		$data = array('password'=>$password);
    		$res = M('gy_user')->where(array('id'=>$id))->setField($data);
    		if ($res) {
    			$arr = array(
                    'error' => 1 ,
                    'info' => '更新成功'
                    );
                echo json_encode($arr);
    		}else {
    			$arr = array(
                    'error' => 0 ,
                    'info' => '更新失败'
                    );
                echo json_encode($arr);
    		}

    	}else {

	    	$id = I('get.id');
	    	$userinfo = M('gy_user')->where(['id'=>$id])->find();
	    	$this->assign('userinfo',$userinfo);

	    	$this->display();
    	}
    }
    //认证表
    public function rz()
    {
        $uid = I('get.id');
        $rz = M('gy_rz_ger')->where(['uid'=>$uid])->find();
        $this->assign('rz',$rz);
        $this->display();
    }
    //认证OK
    public function yesrz()
    {
        if (IS_POST) {
            $id = I('post.rzid');
            $res = M('gy_rz_ger')->where(['id'=>$id])->setField('is_rz',1);
            if ($res) {
                $arr = array('error' => 1 ,'info' => '认证成功');
                echo json_encode($arr);
            }else {
                $arr = array('error' => 0 ,'info' => '认证失败');
                echo json_encode($arr);
            }
        }
    }
    //认证no
    public function norz()
    {
        if (IS_POST) {
            $id = I('post.rzid');
            $res = M('gy_rz_ger')->where(['id'=>$id])->setField('is_rz',0);
            if ($res) {
                $arr = array('error' => 1 ,'info' => '认证取消');
                echo json_encode($arr);
            }else {
                $arr = array('error' => 0 ,'info' => '取消失败');
                echo json_encode($arr);
            }
        }
    }

    //礼品表
    public function lp()
    {
        $uid = I('get.id');
        $this->assign('uid',$uid);

        $lps = M('gy_myljs')->where(['uid'=>$uid])->select();
        $this->assign('lps',$lps);
        $this->display();
    }
    //领取OK
    public function yesly()
    {
        if (IS_POST) {
            $id = I('post.id');
            $data = array('is_sy'=>1,'sytime'=>time());
            $res = M('gy_myljs')->where(['id'=>$id])->setField($data);
            if ($res) {
                $arr = array('error' => 1 ,'info' => '发放成功');
                echo json_encode($arr);
            }else {
                $arr = array('error' => 0 ,'info' => '发放失败');
                echo json_encode($arr);
            }
        }
    }
    //领取no
    public function noly()
    {
        if (IS_POST) {
            $id = I('post.id');
            $data = array('is_sy'=>0,'sytime'=>0);
            $res = M('gy_myljs')->where(['id'=>$id])->setField($data);
            if ($res) {
                $arr = array('error' => 1 ,'info' => '发放取消');
                echo json_encode($arr);
            }else {
                $arr = array('error' => 0 ,'info' => '取消失败');
                echo json_encode($arr);
            }
        }
    }
    //删除礼物
    public function dellw()
    {
        if (IS_POST) {
            $id = I('post.id');
            $res = M('gy_myljs')->delete($id);
            if ($res) {
                $arr = array(
                    'error' => 1 ,
                    'info' => '删除成功'
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0 ,
                    'info' => '删除失败'
                    );
                echo json_encode($arr);
            }
        }
    }
    //add lw
    public function addlw()
    {
        if (IS_POST) {
            $data['uid'] = I('post.uid');
            $data['title'] = I('post.title');
            $data['xioc'] = I('post.xioc');
            $data['time'] = time();

            if (empty($data['title'])) {
                $arr = array('error' => 0 ,'info' => '礼卷名不能为空');
                echo json_encode($arr);
                exit;
            }
            if (empty($data['xioc'])) {
                $arr = array('error' => 0 ,'info' => '标签名不能为空');
                echo json_encode($arr);
                exit;
            }
            $res = M('gy_myljs')->add($data);
            if ($res) {
                $arr = array(
                    'error' => 1 ,
                    'info' => '礼卷添加成功'
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0 ,
                    'info' => '礼卷添加失败'
                    );
                echo json_encode($arr);
            }

        }else {
           $uid = I('get.uid');
           $this->assign('uid',$uid);
           $this->display(); 
        }
    }

    //我的发起
    public function myfq()
    {
        $phone = trim(I('post.phone'));
        if (!empty($phone)) {
            $faqis = M('gy_faqi')->where(['name'=>$phone])->select();
            //数量
            $count = M('gy_faqi')->where(['name'=>$phone])->count();
        }else {
            //分页显示
            $gy_faqi = M('gy_faqi'); // 实例化User对象
            $count      = $gy_faqi->count();// 查询满足要求的总记录数
            $Page       = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $show       = $Page->show();// 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $faqis = $gy_faqi->order('time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
            $this->assign('page',$show);// 赋值分页输出
        }
        $this->assign('count',$count);
        $this->assign('faqis',$faqis);
        
        $this->display();
    }
    //删除 发起
    public function delfaqi()
    {
        if (IS_POST) {
            $id = I('post.id');
            $res = M('gy_faqi')->delete($id);
            if ($res) {
                $arr = array(
                    'error' => 1 ,
                    'info' => '删除成功'
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0 ,
                    'info' => '删除失败'
                    );
                echo json_encode($arr);
            }
        }
    }
    

    //删除
    public function delete()
    {
    	if (IS_POST) {
    		$id = I('post.id');
    		$res = M('gy_user')->delete($id);
    		if ($res) {
    			$arr = array(
                    'error' => 1 ,
                    'info' => '删除成功'
                    );
                echo json_encode($arr);
    		}else {
    			$arr = array(
                    'error' => 0 ,
                    'info' => '删除失败'
                    );
                echo json_encode($arr);
    		}
    	}
    }

}